// exports.xxx 的控制器

// app/controller/upload.js
const Controller = require('egg').Controller;
// const fs = require('mz/fs');
const fs = require('fs');

module.exports = class extends Controller {
  async form1() {
    this.logger.debug('/form/1');
    this.logger.info('/form/1');
    this.logger.warn('/form/1');
    this.logger.error('/form/1');
    await this.ctx.render('home/home/form1.html');
  }
  async form2() {
    await this.ctx.render('home/home/form2.html');
  }
  async form3() {
    await this.ctx.render('home/home/form3.html');
  }
  async form4() {
    await this.ctx.render('home/home/form4.html');
  }

  async upload1() {
    const { ctx } = this;
    this.app.log('upload1', ctx.request.files);
    /*
    [ { field: 'file',
    filename: '11.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\12\\2fee6f74-d7d4-4923-b1c7-a875e322bc9d.jpg' } ]
     */

    // const file = ctx.request.files[0];
    // const name = 'egg-multipart-test/' + path.basename(file.filename);
    // let result;
    // try {
    //   // 处理文件，比如上传到云端
    //   result = await ctx.oss.put(name, file.filepath);
    // } finally {
    //   // 需要删除临时文件
    //   await fs.unlink(file.filepath);
    // }

    // ctx.body = {
    //   url: result.url,
    //   // 获取所有的字段值
    //   requestBody: ctx.request.body,
    // };
    ctx.body = 'upload1';
  }

  async upload2() {
    const { ctx } = this;
    this.app.log('upload2', ctx.request.files);
    /*
    [ { field: 'file',
    filename: '11.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\e7af94c5-6280-4df7-9066-e863521bae7d.jpg' },
  { field: 'file',
    filename: '22.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\6c224805-2062-4ba0-b414-28bd9591b5fb.jpg' },
  { field: 'file',
    filename: '33.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\23a6443f-b922-4045-b542-73e7157d5c63.jpg' } ]
     */
    ctx.body = 'upload2';
  }

  async upload3() {
    const { ctx } = this;
    this.app.log('upload3', ctx.request.files);
    /*
    [ { field: 'file1',
    filename: '11.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\27ec1a1e-7043-470b-babd-1b219cfc4931.jpg' },
  { field: 'file2',
    filename: '22.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\3182741e-8f98-4cd4-866a-42ffeaf3fe71.jpg' },
  { field: 'file3',
    filename: '33.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\ddbe31f2-f013-441d-99d6-e81ca34a53b0.jpg' } ]
     */
    ctx.body = 'upload3';
  }

  async upload4() {
    const { ctx } = this;
    this.app.log('upload4', ctx.request.files);
    /*
    [ { field: 'file1',
    filename: '11.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\6ea11fd9-213d-48a5-b94f-29d78c1d8aa2.jpg' },
  { field: 'file2',
    filename: '22.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\317c2223-e9e9-410f-97a1-00463d266370.jpg' },
  { field: 'file3',
    filename: '33.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\65b474d6-9135-4d55-a6f0-f9fe6d957548.jpg' },
  { field: 'file3',
    filename: '44.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\fa44bd64-22cf-4b3e-8889-0c7f56c50115.jpg' },
  { field: 'file3',
    filename: '55.jpg',
    encoding: '7bit',
    mime: 'image/jpeg',
    filepath: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\egg-multipart-tmp\\example\\2018\\12\\07\\13\\c8c40ada-63ec-475c-ae95-41628c1d7c9d.jpg' } ]
     */
    ctx.body = 'upload4';
  }
};